{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# python login ssh execute command and transfer data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "IPython.notebook.set_autosave_interval(60000)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Autosaving every 60 seconds\n",
      "UnixExclusiveServices\n"
     ]
    }
   ],
   "source": [
    "%autosave 60\n",
    "from dir_objectName import dir_objectName,dir_classification, class_, method_, constant_\n",
    "\n",
    "\n",
    "from print_color import print_color\n",
    "import os, sys, glob, shutil\n",
    "import operator\n",
    "import itertools\n",
    "module = 'getpass'\n",
    "exec(f'import {module}')\n",
    "\n",
    "g= globals()\n",
    "# dir_classification(globals()[module])\n",
    "filename = 'UNIX exclusive services'\n",
    "print(''.join(filename.title().split()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import paramiko    # ssh shell\n",
    "from scp import SCPClient  # scp  file\n",
    "\n",
    "import locale\n",
    "\n",
    "sys_preferred_encoding = locale.getpreferredencoding() # Return the charset that the user is likely using."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cp936'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sys_preferred_encoding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "ssh = paramiko.SSHClient()    \n",
    "# Policy for automatically adding the hostname and new host key to the\n",
    "# local `.HostKeys` object, and saving it.  This is used by `.SSHClient`.\n",
    "ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ssh.connect(hostname=\"192.168.230.129\", port=22, username=\"mach711\", password=getpass.getpass())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir_classification(ssh)\n",
    "\n",
    "list(ssh.get_host_keys())\n",
    "# execute command of linux\n",
    "stdin, stdout, stderr = ssh.exec_command(\"pwd\")\n",
    "\n",
    "stdout.readlines()\n",
    "stdout.read().decode(sys_preferred_encoding)\n",
    "\n",
    "# transfer data fo linux\n",
    "scpclient = SCPClient(ssh.get_transport(), socket_timeout=10.0)\n",
    "## get directory\n",
    "remote_dir = 'Pictures'\n",
    "local_dir = os.getcwd()\n",
    "\n",
    "scpclient.get(remote_dir, local_dir, recursive=True)\n",
    "\n",
    "os.listdir()\n",
    "\n",
    "shutil.rmtree('Pictures')\n",
    "\n",
    "\n",
    "## put directory\n",
    "scpclient.put(files='Pictures', remote_path=remote_dir, recursive=True, preserve_times=True)\n",
    "\n",
    "stdin, stdout, stderr = ssh.exec_command(\"rmy -rf Pictures/Pictures\")\n",
    "stdout"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list(stdout)\n",
    "\n",
    "stderr.readlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir_classification(scpclient)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
